package com.tuni.birthdaymoney.service.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuni.birthdaymoney.persistence.entity.Person;
import com.tuni.birthdaymoney.persistence.entity.User;
import com.tuni.birthdaymoney.persistence.repository.PersonRepository;
import com.tuni.birthdaymoney.persistence.repository.UserRepository;
import com.tuni.birthdaymoney.service.UserService;

@Service
public class SpringUserService implements UserService {

    @Autowired
    private PersonRepository personRepository;
    
    @Autowired
    private UserRepository userRepository;

    @Transactional(readOnly = true)
    public User getUserByEmail(String email) {
        Person person = personRepository.findByEmail(email);
        if (person != null) {
            return person.getUser();
        }
        return null;
    }

    public boolean canAddStaff(User user) {
        if (user != null && user.getRole() != null) {
            return user.getRole().isAdd();
        }
        return false;
    }

    public boolean canUpdateStaff(User user) {
        if (user != null && user.getRole() != null) {
            return user.getRole().isUpdate();
        }
        return false;
    }

    public boolean canDeleteStaff(User user) {
        if (user != null && user.getRole() != null) {
            return user.getRole().isDelete();
        }
        return false;
    }
    
    public void save(User user) {
        userRepository.save(user);
    }
}